package com.heihei.util;

import com.freewayso.image.combiner.ImageCombiner;
import com.freewayso.image.combiner.enums.OutputFormat;
import com.freewayso.image.combiner.enums.ZoomMode;

import java.awt.image.BufferedImage;
import java.io.File;

public class ImageCombinerUtil {
    public static void main(String[] args) throws Exception {
        thumbImg(new File("C:\\Users\\12851\\Desktop\\125\\30.png"),
                "C:\\Users\\12851\\Desktop\\30_out.png", 240);
    }

    /**
     * 获取图片的缩略图
     *
     * @author duanhengfei
     * 2022年08月18日 20:55:47
     */
    public static void thumbImg(File originalImgFile, String targetImgFilePath, int thumbWidth) throws Exception {
//        int thumbWidth = 240;
        ImageCombiner combiner = new ImageCombiner(thumbWidth, thumbWidth, OutputFormat.JPG);
        BufferedImage originalImg = ImgUtil.loadImageLocal(originalImgFile);
        int xStart = 0;
        int yStart = 0;
        int minWidth = originalImg.getWidth();
        if(originalImg.getWidth() > originalImg.getHeight()) {
            xStart = (originalImg.getWidth() - originalImg.getHeight()) / 2;
            minWidth = originalImg.getHeight();
        } else {
            yStart = (originalImg.getHeight() - originalImg.getWidth()) / 2;
        }
        BufferedImage subImage = originalImg.getSubimage(xStart, yStart, minWidth, minWidth);
        combiner.addImageElement(subImage, 0, 0, thumbWidth, thumbWidth, ZoomMode.WidthHeight);
        combiner.combine();
        combiner.save(targetImgFilePath);
    }


}
